package com.ellricky.baselib.ui.order

import android.util.Log
import android.view.View
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ToastUtil
import com.bumptech.glide.request.target.BitmapImageViewTarget
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.ui.xufei.PriceAddActivity
import com.ellricky.baselib.util.*
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.OnInputConfirmListener
import kotlinx.android.synthetic.main.activity_order_detail.*

class OrderDetailActivity : BaseToolBarActivity<Void, BasePresenter<Void>>() {

    private val orderId by lazy {
        intent.getIntExtra("id", -1)
    }

    private var orderDetail: OrderDetail? = null

    override fun getContentLayout(): Int {
        return R.layout.activity_order_detail
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = "订单详情"

        if (LocalCacheUtil.isPresidentRole()) {
            btn_reject.visibility = View.VISIBLE
            btn_submit.visibility = View.VISIBLE
        }

        if (LocalCacheUtil.isTmRole()) {
            btn_modify.visibility = View.VISIBLE
        }

        btn_modify.setOnClickListener {
            orderDetail?.also {
                jump<PriceAddActivity> {
                    putExtra(BaseConstant.KEY_FROM, "student")
                    putExtra(BaseConstant.KEY_ORDER, it)
                }
            }

        }

        btn_submit.setOnClickListener {
            rejectOrder("", BaseConstant.ORDER_CHECK_STATE_PASS.toString())
        }

        btn_reject.setOnClickListener {
            XPopup.Builder(this)
                .asInputConfirm("驳回", "请输入驳回理由") { content ->
                    rejectOrder(content, BaseConstant.ORDER_CHECK_STATE_NOT_PASS.toString())
                }.show()
        }
    }

    override fun onResume() {
        super.onResume()
        loadOrderDetail()
    }

    private fun rejectOrder(content: String?, state: String) {
        if (state == BaseConstant.ORDER_CHECK_STATE_NOT_PASS.toString()) {
            if (content.isNullOrEmpty()) {
                ToastUtil.show(this, "输入内容不能为空")
                return
            }
        }

        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "modifyMemberLogCheckState"
        paramMap["log_id"] = orderId.toString()
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["log_check_state"] = content ?: ""
        paramMap["log_check_state"] = state

        HttpUtils.getInstance().postFrom(url, paramMap, object : NewECallback<Any?>(this) {
            override fun onSuccess(data: Any?, msg: String?) {
                ToastUtil.show(this@OrderDetailActivity, "状态修改成功")
            }
        })
    }

    private fun loadOrderDetail() {
        if (orderId == -1) return
        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "memberLogInfo"
        paramMap["log_id"] = orderId.toString()
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance().postFrom(url, paramMap, object : NewECallback<OrderDetail?>(this) {
            override fun onSuccess(data: OrderDetail?, msg: String?) {
                data?.also {
                    this@OrderDetailActivity.orderDetail = data
                    tv_order_name?.value = it.log_member_name
                    tv_order_product_name?.value = it.log_product_Name
                    tv_order_price.value = it.log_amount

                    tv_classic_time.value = it.log_basic_num.toString() + "节"
                    tv_art_time.value = it.log_special_num.toString() + "节"
                    tv_gift_time.value = it.log_give_num.toString() + "节"
                    tv_history_time.value = it.log_art_num.toString() + "节"

                    val imageList = arrayListOf<String>()
                    if (!it.log_img.isNullOrEmpty()) {
                        imageList.add(it.log_img)
                    }
                    if (!it.log_img2.isNullOrEmpty()) {
                        imageList.add(it.log_img2)
                    }

                    if (!it.log_img3.isNullOrEmpty()) {
                        imageList.add(it.log_img3)
                    }

                    if (!it.log_img.isNullOrEmpty()) {
                        GlideUtil.into(this@OrderDetailActivity, it.log_img, iv_bill_1)
                        iv_bill_1.setOnClickListener {
                            BigIvUtil.jumpBigIvPage(this@OrderDetailActivity, imageList, true, 0)
                        }
                    } else {
                        iv_bill_1.visibility = View.INVISIBLE
                    }

                    if (!it.log_img2.isNullOrEmpty()) {
                        GlideUtil.into(this@OrderDetailActivity, it.log_img2, iv_bill_2)
                        iv_bill_2.setOnClickListener {
                            BigIvUtil.jumpBigIvPage(this@OrderDetailActivity, imageList, true, 1)
                        }
                    } else {
                        iv_bill_2.visibility = View.INVISIBLE
                    }

                    if (!it.log_img3.isNullOrEmpty()) {
                        GlideUtil.into(this@OrderDetailActivity, it.log_img3, iv_bill_3)
                        iv_bill_3.setOnClickListener {
                            BigIvUtil.jumpBigIvPage(this@OrderDetailActivity, imageList, true, 2)
                        }
                    } else {
                        iv_bill_3.visibility = View.INVISIBLE
                    }

                    if ((it.log_confirm_state == BaseConstant.ORDER_CHECK_STATE_PASS || it.log_confirm_state == BaseConstant.ORDER_CHECK_STATE_NOT_PASS) && LocalCacheUtil.isPresidentRole()) {
                        btn_reject.visibility = View.GONE
                        btn_submit.visibility = View.GONE
                    }

                    if (it.log_check_state == 1 && LocalCacheUtil.isTmRole()) {
                        btn_modify.visibility = View.GONE
                    }
                }
            }
        })
    }
}